# apps/zhihu/serializers.py

from rest_framework import serializers
from .models import ZhihuContent, ZhihuComment
from markdown2 import markdown
import re
from django.utils.html import strip_tags
from utils.timestamp_change import convert_timestamp


class ZhihuCommentSerializer(serializers.ModelSerializer):
    add_ts = serializers.SerializerMethodField()
    last_modify_ts = serializers.SerializerMethodField()
    publish_time = serializers.SerializerMethodField()
    children = serializers.SerializerMethodField()

    class Meta:
        model = ZhihuComment
        fields = [
            'comment_id',
            'parent_comment_id',
            'content',
            'publish_time',
            'ip_location',
            'sub_comment_count',
            'like_count',
            'dislike_count',
            'content_id',
            'content_type',
            'user_id',
            'user_link',
            'user_nickname',
            'user_avatar',
            'add_ts',
            'last_modify_ts',
            'children',
        ]

    def get_add_ts(self, obj):
        return convert_timestamp(obj.add_ts)

    def get_last_modify_ts(self, obj):
        return convert_timestamp(obj.last_modify_ts)

    def get_publish_time(self, obj):
        return convert_timestamp(obj.publish_time)

    def get_children(self, obj):
        children = self.context.get('children', {}).get(obj.comment_id, [])
        return ZhihuCommentSerializer(children, many=True, context=self.context).data

class ZhihuContentSerializer(serializers.ModelSerializer):
    comments = serializers.SerializerMethodField()
    add_ts = serializers.SerializerMethodField()
    last_modify_ts = serializers.SerializerMethodField()
    created_time = serializers.SerializerMethodField()
    updated_time = serializers.SerializerMethodField()
    content_text = serializers.SerializerMethodField()

    class Meta:
        model = ZhihuContent
        fields = [
            'content_id',
            'content_type',
            'content_text',
            'content_url',
            'question_id',
            'title',
            'desc',
            'created_time',
            'updated_time',
            'voteup_count',
            'comment_count',
            'source_keyword',
            'user_id',
            'user_link',
            'user_nickname',
            'user_avatar',
            'user_url_token',
            'add_ts',
            'last_modify_ts',
            'comments',
        ]

    def get_add_ts(self, obj):
        return convert_timestamp(obj.add_ts)

    def get_last_modify_ts(self, obj):
        return convert_timestamp(obj.last_modify_ts)

    def get_created_time(self, obj):
        return convert_timestamp(obj.created_time)

    def get_updated_time(self, obj):
        return convert_timestamp(obj.updated_time)
    
    def get_content_text(self, obj):
        content_text = obj.content_text
        if content_text:
            # 将Markdown格式的文章内容转换为HTML
            html_content = markdown(content_text)

            # 提取图片URL
            image_urls = re.findall(r'<img.*?src="(.*?)"', html_content)

            # 提取代码块
            code_blocks = re.findall(r'<pre><code>(.*?)</code></pre>', html_content, re.DOTALL)

            # 提取段落文本
            paragraphs = re.findall(r'<p>(.*?)</p>', html_content)
            paragraph_texts = [strip_tags(p) for p in paragraphs]

            # 构建优化后的文章内容
            optimized_content = {
                'images': image_urls,
                'code_blocks': code_blocks,
                'paragraphs': paragraph_texts
            }

            return optimized_content
        return None

    def get_comments(self, obj):
        comment_tree = self.context.get('comment_tree', [])
        return ZhihuCommentSerializer(comment_tree, many=True, context=self.context).data